<?php
class Bizprofile_Widgets_Blog_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $lang = $params['lang'];

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Enterprise basic model access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);
        $blogDao->setLang('vi_VN');

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Get current enterprise
        **/
        //$objEnterprise = $enterpriseDao->getById($enterprise_id);
        //$this->_view->assign('enterprise', $objEnterprise);

        /**
        * Get latest blog
        **/
        $exp = array("enterprise_frontend_active"=>1);
        $blogs = $blogDao->find(0, 10, $exp);
        $arrBlogs = array();
        foreach($blogs as $key=>$blog){
            /**
             * check if blog en exist
            **/
            $arrBlogs[$key] = $blog;
            if($lang == 'en_US'){
                $objBlog = $blogDao->getById($blog->blog_id, 'en_US');
                if($objBlog){
                    $arrBlogs[$key] = $objBlog;
                }
            }
        }
        $this->_view->assign('blogs', $arrBlogs);

        /**
         * Get enterprise follow blog
        **/
        $arrBE = array();
        foreach($arrBlogs as $blog){
            $enterprise = $enterpriseDao->getById($blog->enterprise_id);
            $arrBE[$blog->enterprise_id] = $enterprise;
        }
        $this->_view->assign('arrBE', $arrBE);
    }

    protected function _prepareConfig()
    {
    }
}
